#ifndef _SPAWNS_H_
#define _SPAWNS_H_


#include "Common.h"
#include "String.h"
#include "SharedPtr.h"
#include "Resource.h"
#include "Vector3.h"
#include "Effects.h"
#include "Rect.h"

class ItemPtr;
class EffectPtr;
class CharPtr;

//物品公共属性
typedef struct spawn_common_property
{

	ResourceProxy		uplink;		//地图 Region 对象
	void initialize(void)
	{
		
		uplink.setNull();
	}

	spawn_common_property()
	{
		initialize();
	}

}spawn_common_property_t;

class  Spawn:public Resource
{
public:
	enum SpawnStatus
	{
		SpawnStatusNone			= 0,	//无效状态
		SpawnStatusNormal		= 1,	//一般状态
		SpawnStatusFreeze		= 2,	//冻结状态
		SpawnStatusDeleted		= 255	//删除状态
	};
	
	enum SpawnType
	{
		SpawnTypeNone			= 0,	//无效类型
		SpawnTypeItem			= 1,	//物品再生
		SpawnTypeCreature		= 2,	//角色再生
		SpawnTypeHideItem		= 3,	//暗物再生
		SpawnTypeHideCreature		= 4	//暗怪再生
	};

	enum SpawnStyle
	{
		SpawnStyleNone			= 0,	//无效模式
		SpawnStylePoint			= 1,	//点再生
		SpawnStyleCycle			= 2,	//圆形半径再生
		SpawnStyleRect			= 3	//矩形区域再生
	};

	Spawn(ResourceManager* creator, const String& name, ResourceHandle handle,
            const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
	~Spawn(void);

protected:
       	void prepareImpl(void);
        /** Destroys data cached by prepareImpl.
         */
        void unprepareImpl(void);
        /// @copydoc Resource::loadImpl
        bool loadImpl(void);
        /// @copydoc Resource::postLoadImpl
        void postLoadImpl(void);

	 void preUnloadImpl(void);
        /// @copydoc Resource::unloadImpl
        void unloadImpl(void);
        /// @copydoc Resource::calculateSize
        size_t calculateSize(void) const;

private:
	std::list<ResourceProxy>	m_spawns;	//包含的物品序列号
	
	uint32		m_serial;

	Rect		m_spawn_rect;	//再生范围，全部折算为矩形区域
	bool            m_idle;
	
	void    initialize(void);
public:

	const uint32  & getSerial() 	{	return m_serial;	}


	//添加释放创造物
	//bool	addSpawn(const uint32 & serial);
	bool  	addSpawn(ResourceProxy & spawn);
	//bool	removeSpawn(const uint32 & serial);
	bool	removeSpawn(ResourceProxy & spawn);
	//bool	deleteSpawn(const uint32 & serial);
	bool	deleteSpawn(ResourceProxy & spawn);
	//bool	destroySpawn(const uint32 & serial);
	bool	destroySpawn(ResourceProxy & spawn);
	void	removeAllSpawns(void);
	void	destroyAllSpawns(void);
	void	deleteAllSpawns(void);

	//得到当前再生物的数量
	size_t	getSpawnNum(void);


	//得到再生创造物列表
	std::list<ResourceProxy> * getSpawnsList(std::list<ResourceProxy> * list);

	const SpawnStatus  getSpawnStatus(void);
	void setSpawnStatus(const SpawnStatus & status);
	
	//是否为隐藏怪或者物品
	bool  isHideSpawn(void);

	//得到再生区域
	const Rect & getSpawnRect(void)		{	return m_spawn_rect;	}

	//save item to database	
	bool	SaveDB(void);
	bool	LoadDB(void);
	bool    FetchDB(Field * fields);

	//设置再生处理标识为忙，避免重复队列处理
        void    setBusy(void)           {       m_idle = false; }
        void    setIdle(void)           {       m_idle = true;  }
        bool    isBusy(void)            {       return (m_idle == false);       }


	void 	Delete(void);
	void 	Destroy(void);

	//国家公共属性
	spawn_common_property_t	common;
};

    /** Specialisation of SharedPtr to allow SharedPtr to be assigned to SpawnPtr 
    @note Has to be a subclass since we need operator=.
    We could templatise this instead of repeating per Resource subclass, 
    except to do so requires a form VC6 does not support i.e.
    ResourceSubclassPtr<T> : public SharedPtr<T>
    */
    class SERVER_DECL  SpawnPtr : public SharedPtr<Spawn>
    {
    public:
        SpawnPtr() : SharedPtr<Spawn>() {}
        explicit SpawnPtr(Spawn* rep) : SharedPtr<Spawn>(rep) {}
        SpawnPtr(const SpawnPtr& r) : SharedPtr<Spawn>(r) {}
        SpawnPtr(const ResourcePtr& r);
        /// Operator used to convert a ResourcePtr to a MeshPtr
        SpawnPtr& operator=(const ResourcePtr& r);
    protected:
        /// Override destroy since we need to delete Mesh after fully defined
        void destroy(void);
    };
	

#endif
